package com.example.shop.fgo.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.shop.library.net.NetJsonUtil;
import com.example.shop.library.util.UtilEdt;
import com.example.shop.fgo.Constant;
import com.example.shop.fgo.R;
import com.example.shop.fgo.entity.FileListEntity;
import com.example.shop.fgo.entity.SearchEntity;
import com.example.shop.fgo.net.SearchServe;
import com.example.shop.fgo.ui.fragment.WPFragment;
import com.example.shop.fgo.utils.CookieUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/2/10.
 */

public class DigSearch extends Dialog {
    
    private EditText mEdtContent;
    private Button mBtCancel;
    private Button mBtConfirm;
    private WPFragment mWPFragment;
    
    public DigSearch(final Context context, WPFragment wpFragment) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mWPFragment=wpFragment;
        setContentView(R.layout.dig_search);
        
        mEdtContent = (EditText) findViewById(R.id.edt_content);
        mBtCancel = (Button) findViewById(R.id.bt_cancel);
        mBtConfirm = (Button) findViewById(R.id.bt_confirm);
    
        mBtCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancel();
        
            }
        });
        
        mBtConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content= UtilEdt.getEdtText(mEdtContent);
    
                if (TextUtils.isEmpty(content)) {
                    Toast.makeText(context, "关键字不能为空！！", Toast.LENGTH_SHORT).show();
                    return;
                }
                
                doNet(content);
            }
        });
    }
    
    private void doNet(final String content) {
        
        SearchServe searchServe = NetJsonUtil.getRetrofit(Constant.BASE_URL, SearchServe.class);
//        offset=0&limit=28&value=bbb
        Map<String ,Object> params=new HashMap<>();
        params.put("offset",0);
        params.put("limit",666);
        params.put("value",content);
        Call<com.example.shop.fgo.entity.SearchEntity> call = searchServe.request(params, CookieUtil.getCookie());

        call.enqueue(new Callback<SearchEntity>() {
            @Override
            public void onResponse(Call<SearchEntity> call, Response<SearchEntity> response) {
                SearchEntity entity = response.body();
                if (entity.isState()) {
                    Toast.makeText(getContext(), "搜索成功！", Toast.LENGTH_SHORT).show();
                    cancel();
                    mWPFragment.mItemLvFileAdapter.setEntities(entity.getData());
    
                    FileListEntity.PathEntity pathEntity=new FileListEntity.PathEntity();
                    pathEntity.setName(content+" 的搜索结果");
                    pathEntity.setSearch(true);
                    List<FileListEntity.PathEntity> newPathEntities=new ArrayList<FileListEntity.PathEntity>(mWPFragment.mPathEntities.size());
                    
                    
                    for (FileListEntity.PathEntity pathEntity1 : mWPFragment.mPathEntities) {
                        newPathEntities.add(pathEntity1);
                    }
                    newPathEntities.add(pathEntity);
                    mWPFragment.mPathNavi.setPathEntities(newPathEntities);
                }else {
                    Toast.makeText(getContext(), "搜索失败！", Toast.LENGTH_SHORT).show();
                }
            }
    
            @Override
            public void onFailure(Call<SearchEntity> call, Throwable t) {
                Toast.makeText(getContext(), "网络连接失败", Toast.LENGTH_SHORT).show();
            }
        });
    }
    
}
